<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Form Assets (the Media class) &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Working with forms" href="index.html" />
    <link rel="next" title="The Django template language" href="../templates.html" />
    <link rel="prev" title="Creating forms from models" href="modelforms.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="modelforms.html" title="Creating forms from models">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="../templates.html" title="The Django template language">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-forms-media">
            
  <div class="section" id="s-form-assets-the-media-class">
<span id="form-assets-the-media-class"></span><h1>Form Assets (the <tt class="docutils literal"><span class="pre">Media</span></tt> class)<a class="headerlink" href="#form-assets-the-media-class" title="Permalink to this headline">¶</a></h1>
<p>Rendering an attractive and easy-to-use Web form requires more than just
HTML - it also requires CSS stylesheets, and if you want to use fancy
&#8220;Web2.0&#8221; widgets, you may also need to include some JavaScript on each
page. The exact combination of CSS and JavaScript that is required for
any given page will depend upon the widgets that are in use on that page.</p>
<p>This is where asset definitions come in. Django allows you to
associate different files &#8211; like stylesheets and scripts &#8211; with the
forms and widgets that require those assets. For example, if you want
to use a calendar to render DateFields, you can define a custom
Calendar widget. This widget can then be associated with the CSS and
JavaScript that is required to render the calendar. When the Calendar
widget is used on a form, Django is able to identify the CSS and
JavaScript files that are required, and provide the list of file names
in a form suitable for easy inclusion on your Web page.</p>
<div class="admonition-assets-and-django-admin admonition">
<p class="first admonition-title">Assets and Django Admin</p>
<p>The Django Admin application defines a number of customized
widgets for calendars, filtered selections, and so on. These
widgets define asset requirements, and the Django Admin uses the
custom widgets in place of the Django defaults. The Admin
templates will only include those files that are required to
render the widgets on any given page.</p>
<p class="last">If you like the widgets that the Django Admin application uses,
feel free to use them in your own application! They&#8217;re all stored
in <tt class="docutils literal"><span class="pre">django.contrib.admin.widgets</span></tt>.</p>
</div>
<div class="admonition-which-javascript-toolkit admonition">
<p class="first admonition-title">Which JavaScript toolkit?</p>
<p class="last">Many JavaScript toolkits exist, and many of them include widgets (such
as calendar widgets) that can be used to enhance your application.
Django has deliberately avoided blessing any one JavaScript toolkit.
Each toolkit has its own relative strengths and weaknesses - use
whichever toolkit suits your requirements. Django is able to integrate
with any JavaScript toolkit.</p>
</div>
<div class="section" id="s-assets-as-a-static-definition">
<span id="s-id1"></span><span id="assets-as-a-static-definition"></span><span id="id1"></span><h2>Assets as a static definition<a class="headerlink" href="#assets-as-a-static-definition" title="Permalink to this headline">¶</a></h2>
<p>The easiest way to define assets is as a static definition. Using this
method, the declaration is an inner <tt class="docutils literal"><span class="pre">Media</span></tt> class. The properties of the
inner class define the requirements.</p>
<p>Here&#8217;s a simple example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">CalendarWidget</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">TextInput</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;pretty.css&#39;</span><span class="p">,)</span>
        <span class="p">}</span>
        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;animations.js&#39;</span><span class="p">,</span> <span class="s">&#39;actions.js&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This code defines a <tt class="docutils literal"><span class="pre">CalendarWidget</span></tt>, which will be based on <tt class="docutils literal"><span class="pre">TextInput</span></tt>.
Every time the CalendarWidget is used on a form, that form will be directed
to include the CSS file <tt class="docutils literal"><span class="pre">pretty.css</span></tt>, and the JavaScript files
<tt class="docutils literal"><span class="pre">animations.js</span></tt> and <tt class="docutils literal"><span class="pre">actions.js</span></tt>.</p>
<p>This static definition is converted at runtime into a widget property
named <tt class="docutils literal"><span class="pre">media</span></tt>. The list of assets for a <tt class="docutils literal"><span class="pre">CalendarWidget</span></tt> instance
can be retrieved through this property:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">CalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
<p>Here&#8217;s a list of all possible <tt class="docutils literal"><span class="pre">Media</span></tt> options. There are no required options.</p>
<div class="section" id="s-css">
<span id="css"></span><h3><tt class="docutils literal"><span class="pre">css</span></tt><a class="headerlink" href="#css" title="Permalink to this headline">¶</a></h3>
<p>A dictionary describing the CSS files required for various forms of output
media.</p>
<p>The values in the dictionary should be a tuple/list of file names. See
<a class="reference internal" href="#form-asset-paths"><em>the section on paths</em></a> for details of how to
specify paths to these files.</p>
<p>The keys in the dictionary are the output media types. These are the same
types accepted by CSS files in media declarations: &#8216;all&#8217;, &#8216;aural&#8217;, &#8216;braille&#8217;,
&#8216;embossed&#8217;, &#8216;handheld&#8217;, &#8216;print&#8217;, &#8216;projection&#8217;, &#8216;screen&#8217;, &#8216;tty&#8217; and &#8216;tv&#8217;. If
you need to have different stylesheets for different media types, provide
a list of CSS files for each output medium. The following example would
provide two CSS options &#8211; one for the screen, and one for print:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
    <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;screen&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;pretty.css&#39;</span><span class="p">,),</span>
        <span class="s">&#39;print&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;newspaper.css&#39;</span><span class="p">,)</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>If a group of CSS files are appropriate for multiple output media types,
the dictionary key can be a comma separated list of output media types.
In the following example, TV&#8217;s and projectors will have the same media
requirements:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
    <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;screen&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;pretty.css&#39;</span><span class="p">,),</span>
        <span class="s">&#39;tv,projector&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;lo_res.css&#39;</span><span class="p">,),</span>
        <span class="s">&#39;print&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;newspaper.css&#39;</span><span class="p">,)</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>If this last CSS definition were to be rendered, it would become the following HTML:</p>
<div class="highlight-python"><div class="highlight"><pre>&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;link href=&quot;http://static.example.com/lo_res.css&quot; type=&quot;text/css&quot; media=&quot;tv,projector&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;link href=&quot;http://static.example.com/newspaper.css&quot; type=&quot;text/css&quot; media=&quot;print&quot; rel=&quot;stylesheet&quot; /&gt;
</pre></div>
</div>
</div>
<div class="section" id="s-js">
<span id="js"></span><h3><tt class="docutils literal"><span class="pre">js</span></tt><a class="headerlink" href="#js" title="Permalink to this headline">¶</a></h3>
<p>A tuple describing the required JavaScript files. See <a class="reference internal" href="#form-asset-paths"><em>the
section on paths</em></a> for details of how to specify
paths to these files.</p>
</div>
<div class="section" id="s-extend">
<span id="extend"></span><h3><tt class="docutils literal"><span class="pre">extend</span></tt><a class="headerlink" href="#extend" title="Permalink to this headline">¶</a></h3>
<p>A boolean defining inheritance behavior for <tt class="docutils literal"><span class="pre">Media</span></tt> declarations.</p>
<p>By default, any object using a static <tt class="docutils literal"><span class="pre">Media</span></tt> definition will
inherit all the assets associated with the parent widget. This occurs
regardless of how the parent defines its own requirements. For
example, if we were to extend our basic Calendar widget from the
example above:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">FancyCalendarWidget</span><span class="p">(</span><span class="n">CalendarWidget</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>            <span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;fancy.css&#39;</span><span class="p">,)</span>
<span class="gp">... </span>        <span class="p">}</span>
<span class="gp">... </span>        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;whizbang.js&#39;</span><span class="p">,)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">FancyCalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;link href=&quot;http://static.example.com/fancy.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
<p>The FancyCalendar widget inherits all the assets from its parent
widget. If you don&#8217;t want <tt class="docutils literal"><span class="pre">Media</span></tt> to be inherited in this way, add
an <tt class="docutils literal"><span class="pre">extend=False</span></tt> declaration to the <tt class="docutils literal"><span class="pre">Media</span></tt> declaration:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">FancyCalendarWidget</span><span class="p">(</span><span class="n">CalendarWidget</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">extend</span> <span class="o">=</span> <span class="bp">False</span>
<span class="gp">... </span>        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>            <span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;fancy.css&#39;</span><span class="p">,)</span>
<span class="gp">... </span>        <span class="p">}</span>
<span class="gp">... </span>        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;whizbang.js&#39;</span><span class="p">,)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">FancyCalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;http://static.example.com/fancy.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
<p>If you require even more control over inheritance, define your assets using a
<a class="reference internal" href="#dynamic-property"><em>dynamic property</em></a>. Dynamic properties give you
complete control over which files are inherited, and which are not.</p>
</div>
</div>
<div class="section" id="s-media-as-a-dynamic-property">
<span id="s-dynamic-property"></span><span id="media-as-a-dynamic-property"></span><span id="dynamic-property"></span><h2><tt class="docutils literal"><span class="pre">Media</span></tt> as a dynamic property<a class="headerlink" href="#media-as-a-dynamic-property" title="Permalink to this headline">¶</a></h2>
<p>If you need to perform some more sophisticated manipulation of asset
requirements, you can define the <tt class="docutils literal"><span class="pre">media</span></tt> property directly. This is
done by defining a widget property that returns an instance of
<tt class="docutils literal"><span class="pre">forms.Media</span></tt>.  The constructor for <tt class="docutils literal"><span class="pre">forms.Media</span></tt> accepts <tt class="docutils literal"><span class="pre">css</span></tt>
and <tt class="docutils literal"><span class="pre">js</span></tt> keyword arguments in the same format as that used in a
static media definition.</p>
<p>For example, the static definition for our Calendar Widget could also
be defined in a dynamic fashion:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CalendarWidget</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">TextInput</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">_media</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">forms</span><span class="o">.</span><span class="n">Media</span><span class="p">(</span><span class="n">css</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;pretty.css&#39;</span><span class="p">,)},</span>
                           <span class="n">js</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;animations.js&#39;</span><span class="p">,</span> <span class="s">&#39;actions.js&#39;</span><span class="p">))</span>
    <span class="n">media</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_media</span><span class="p">)</span>
</pre></div>
</div>
<p>See the section on <a class="reference internal" href="#media-objects">Media objects</a> for more details on how to construct
return values for dynamic <tt class="docutils literal"><span class="pre">media</span></tt> properties.</p>
</div>
<div class="section" id="s-paths-in-asset-definitions">
<span id="s-form-asset-paths"></span><span id="paths-in-asset-definitions"></span><span id="form-asset-paths"></span><h2>Paths in asset definitions<a class="headerlink" href="#paths-in-asset-definitions" title="Permalink to this headline">¶</a></h2>
<p>Paths used to specify assets can be either relative or absolute. If a
path starts with <tt class="docutils literal"><span class="pre">/</span></tt>, <tt class="docutils literal"><span class="pre">http://</span></tt> or <tt class="docutils literal"><span class="pre">https://</span></tt>, it will be
interpreted as an absolute path, and left as-is. All other paths will
be prepended with the value of the appropriate prefix.</p>
<p>As part of the introduction of the
<a class="reference internal" href="../../ref/contrib/staticfiles.html"><em>staticfiles app</em></a> two new settings were added
to refer to &#8220;static files&#8221; (images, CSS, Javascript, etc.) that are needed
to render a complete web page: <a class="reference internal" href="../../ref/settings.html#std:setting-STATIC_URL"><tt class="xref std std-setting docutils literal"><span class="pre">STATIC_URL</span></tt></a> and <a class="reference internal" href="../../ref/settings.html#std:setting-STATIC_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">STATIC_ROOT</span></tt></a>.</p>
<p>To find the appropriate prefix to use, Django will check if the
<a class="reference internal" href="../../ref/settings.html#std:setting-STATIC_URL"><tt class="xref std std-setting docutils literal"><span class="pre">STATIC_URL</span></tt></a> setting is not <tt class="docutils literal"><span class="pre">None</span></tt> and automatically fall back
to using <a class="reference internal" href="../../ref/settings.html#std:setting-MEDIA_URL"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></tt></a>. For example, if the <a class="reference internal" href="../../ref/settings.html#std:setting-MEDIA_URL"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></tt></a> for
your site was <tt class="docutils literal"><span class="pre">'http://uploads.example.com/'</span></tt> and <a class="reference internal" href="../../ref/settings.html#std:setting-STATIC_URL"><tt class="xref std std-setting docutils literal"><span class="pre">STATIC_URL</span></tt></a>
was <tt class="docutils literal"><span class="pre">None</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CalendarWidget</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">TextInput</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>            <span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;/css/pretty.css&#39;</span><span class="p">,),</span>
<span class="gp">... </span>        <span class="p">}</span>
<span class="gp">... </span>        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;animations.js&#39;</span><span class="p">,</span> <span class="s">&#39;http://othersite.com/actions.js&#39;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">CalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;/css/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://uploads.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
<p>But if <a class="reference internal" href="../../ref/settings.html#std:setting-STATIC_URL"><tt class="xref std std-setting docutils literal"><span class="pre">STATIC_URL</span></tt></a> is <tt class="docutils literal"><span class="pre">'http://static.example.com/'</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">CalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;/css/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-media-objects">
<span id="media-objects"></span><h2><tt class="docutils literal"><span class="pre">Media</span></tt> objects<a class="headerlink" href="#media-objects" title="Permalink to this headline">¶</a></h2>
<p>When you interrogate the <tt class="docutils literal"><span class="pre">media</span></tt> attribute of a widget or form, the
value that is returned is a <tt class="docutils literal"><span class="pre">forms.Media</span></tt> object. As we have already
seen, the string representation of a <tt class="docutils literal"><span class="pre">Media</span></tt> object is the HTML
required to include the relevant files in the <tt class="docutils literal"><span class="pre">&lt;head&gt;</span></tt> block of your
HTML page.</p>
<p>However, <tt class="docutils literal"><span class="pre">Media</span></tt> objects have some other interesting properties.</p>
<div class="section" id="s-subsets-of-assets">
<span id="subsets-of-assets"></span><h3>Subsets of assets<a class="headerlink" href="#subsets-of-assets" title="Permalink to this headline">¶</a></h3>
<p>If you only want files of a particular type, you can use the subscript
operator to filter out a medium of interest. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">CalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">media</span><span class="p">[</span><span class="s">&#39;css&#39;</span><span class="p">])</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
</pre></div>
</div>
<p>When you use the subscript operator, the value that is returned is a
new <tt class="docutils literal"><span class="pre">Media</span></tt> object &#8211; but one that only contains the media of interest.</p>
</div>
<div class="section" id="s-combining-media-objects">
<span id="combining-media-objects"></span><h3>Combining <tt class="docutils literal"><span class="pre">Media</span></tt> objects<a class="headerlink" href="#combining-media-objects" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">Media</span></tt> objects can also be added together. When two <tt class="docutils literal"><span class="pre">Media</span></tt> objects are
added, the resulting <tt class="docutils literal"><span class="pre">Media</span></tt> object contains the union of the assets
specified by both:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">CalendarWidget</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">TextInput</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>            <span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;pretty.css&#39;</span><span class="p">,)</span>
<span class="gp">... </span>        <span class="p">}</span>
<span class="gp">... </span>        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;animations.js&#39;</span><span class="p">,</span> <span class="s">&#39;actions.js&#39;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">OtherWidget</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">TextInput</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;whizbang.js&#39;</span><span class="p">,)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">w1</span> <span class="o">=</span> <span class="n">CalendarWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w2</span> <span class="o">=</span> <span class="n">OtherWidget</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">w1</span><span class="o">.</span><span class="n">media</span> <span class="o">+</span> <span class="n">w2</span><span class="o">.</span><span class="n">media</span><span class="p">)</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-media-on-forms">
<span id="media-on-forms"></span><h2><tt class="docutils literal"><span class="pre">Media</span></tt> on Forms<a class="headerlink" href="#media-on-forms" title="Permalink to this headline">¶</a></h2>
<p>Widgets aren&#8217;t the only objects that can have <tt class="docutils literal"><span class="pre">media</span></tt> definitions &#8211;
forms can also define <tt class="docutils literal"><span class="pre">media</span></tt>. The rules for <tt class="docutils literal"><span class="pre">media</span></tt> definitions
on forms are the same as the rules for widgets: declarations can be
static or dynamic; path and inheritance rules for those declarations
are exactly the same.</p>
<p>Regardless of whether you define a <tt class="docutils literal"><span class="pre">media</span></tt> declaration, <em>all</em> Form
objects have a <tt class="docutils literal"><span class="pre">media</span></tt> property. The default value for this property
is the result of adding the <tt class="docutils literal"><span class="pre">media</span></tt> definitions for all widgets that
are part of the form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">date</span> <span class="o">=</span> <span class="n">DateField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">CalendarWidget</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="n">OtherWidget</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">media</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
<p>If you want to associate additional assets with a form &#8211; for example,
CSS for form layout &#8211; simply add a <tt class="docutils literal"><span class="pre">Media</span></tt> declaration to the
form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">date</span> <span class="o">=</span> <span class="n">DateField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">CalendarWidget</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="n">OtherWidget</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
<span class="gp">... </span>            <span class="s">&#39;all&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;layout.css&#39;</span><span class="p">,)</span>
<span class="gp">... </span>        <span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">ContactForm</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">media</span>
<span class="go">&lt;link href=&quot;http://static.example.com/pretty.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;link href=&quot;http://static.example.com/layout.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot; /&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>
<span class="go">&lt;script type=&quot;text/javascript&quot; src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Form Assets (the <tt class="docutils literal"><span class="pre">Media</span></tt> class)</a><ul>
<li><a class="reference internal" href="#assets-as-a-static-definition">Assets as a static definition</a><ul>
<li><a class="reference internal" href="#css"><tt class="docutils literal"><span class="pre">css</span></tt></a></li>
<li><a class="reference internal" href="#js"><tt class="docutils literal"><span class="pre">js</span></tt></a></li>
<li><a class="reference internal" href="#extend"><tt class="docutils literal"><span class="pre">extend</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#media-as-a-dynamic-property"><tt class="docutils literal"><span class="pre">Media</span></tt> as a dynamic property</a></li>
<li><a class="reference internal" href="#paths-in-asset-definitions">Paths in asset definitions</a></li>
<li><a class="reference internal" href="#media-objects"><tt class="docutils literal"><span class="pre">Media</span></tt> objects</a><ul>
<li><a class="reference internal" href="#subsets-of-assets">Subsets of assets</a></li>
<li><a class="reference internal" href="#combining-media-objects">Combining <tt class="docutils literal"><span class="pre">Media</span></tt> objects</a></li>
</ul>
</li>
<li><a class="reference internal" href="#media-on-forms"><tt class="docutils literal"><span class="pre">Media</span></tt> on Forms</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="modelforms.html">Creating forms from models</a></li>
    
    
      <li>Next: <a href="../templates.html">The Django template language</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">Working with forms</a>
        
        <ul><li>Form Assets (the <tt class="docutils literal"><span class="pre">Media</span></tt> class)</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/forms/media.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="modelforms.html" title="Creating forms from models">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="../templates.html" title="The Django template language">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>